package com.atguigu.crowd.mvc.handler;


import com.atguigu.crowd.entity.Auth;
import com.atguigu.crowd.entity.Role;
import com.atguigu.crowd.service.api.AdminService;
import com.atguigu.crowd.service.api.AuthService;
import com.atguigu.crowd.service.api.RoleService;
import com.atguigu.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class AssignHandler {

    @Autowired
    private RoleService roleService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private AuthService authService;

    @RequestMapping("/assign/do/role/assign/auth.json")
    @ResponseBody
    public ResultEntity<String> saveRoleAuthRelathinship(@RequestBody Map<String,List<Integer>> map){

        authService.saveRoleAuthRelathinship(map);

        return ResultEntity.successWithoutData();
    }

    @RequestMapping("/assign/get/assigned/auth/id/by/role/id.json")
    @ResponseBody
    public ResultEntity<List<Integer>> getAssignedAuthIdByRoleId(@RequestParam("roleId") Integer roleId){

        System.out.println(roleId);
        List<Integer> authIdList=authService.getAssignedAuthIdByRoleId(roleId);

        return ResultEntity.successWithoutData(authIdList);
    }

    @RequestMapping("/assgin/get/all/auth.json")
    @ResponseBody
    public ResultEntity<List<Auth>> getAll(){

        List<Auth> authList=authService.getAll();


        return ResultEntity.successWithoutData(authList);
    }


    @RequestMapping("/assign/do/role/assign.html")
    public String saveAdminRoleRelationship(
            @RequestParam("adminId") Integer adminId,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("keyword") String keyword,
            @RequestParam("roleIdList") List<Integer> roleIdList

    ){

        adminService.saveAdminRoleRelationship(adminId,roleIdList);

        return "redirect:/admin/get/page.html?pageNum="+pageNum+"&keyword="+keyword;

    }

    @RequestMapping("/assign/to/assign/role/page.html")
    public String toAssignRolePage(@RequestParam("adminId") Integer adminId, ModelMap modelMap){

       List<Role> assignRoleList= roleService.getAssignRole(adminId);


       List<Role> unAssignRoleList= roleService.getUnAssignRole(adminId);

       modelMap.addAttribute("assignRoleList",assignRoleList);
       modelMap.addAttribute("unAssignRoleList",unAssignRoleList);

       return "assign-role";


    }
}
